<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字游戏</title>
</head>
<body>
<button id="resetButton">重新开始一局游戏</button>
<br>
<span>请输入要猜的数字: </span>
<input type="text">
<button id="guessButton">猜</button>
<br>
<span>已经猜的次数: </span> <span id="guessCount">0</span>
<br>
<span>结果: </span><span id="result"></span>

<script>
        // 1、先把上面需要用到元素都拿到
        let resetButton = document.querySelector('#resetButton');
        let input = document.querySelector('input');
        let guessButton = document.querySelector('#guessButton');
        let guessCount = document.querySelector('#guessCount');
        let result = document.querySelector('#result');
        // 2、生成一个 1-100的 随机数
        let toGuess = Math.floor(Math.random( ) * 100) + 1
        console.log(toGuess);

        // 3、实现点击 猜按钮的逻辑
        guessButton.onclick = function(){
            // 1、读取到input中输入的内容，并转换为整数
            if(input.value == ''){
                return;
            }
            let currentNum = parseInt(input.value);
            // 2、进行判断大小关系，并给出提示
            if(currentNum < toGuess){
               result.innerHTML = '猜小了';
               result.style.color ='orange';
            }else if(currentNum > toGuess){
                result.innerHTML = "猜大了!";
                result.style.color = 'red';
            }else{
                result.innerHTML = '猜对了！';
                result.style.color = 'green';
            }
            // 3、更新猜的次数。
            let guessC = parseInt(guessCount.innerHTML);
            guessCount.innerHTML = guessC+1;
        }

        // 4、实现 reset 重置按钮的操作
        resetButton.onclick = function(){

        }
    </script>
</body>
</html>
